package com.wsk.demo.controller;

import com.wsk.api.bean.AdminUser;
import com.wsk.api.bean.SimpleUser;
import com.wsk.api.bean.User;
import com.wsk.api.bean.UserEnum;
import com.wsk.api.controller.UserController;
import com.wsk.api.service.ManagerUser;
import com.wsk.api.service.UserLogin;
import com.wsk.api.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @DESCRIPTION :
 * @AUTHOR : WuShukai1103
 * @TIME : 2017/12/9  23:59
 */
//@RestController
@Controller
//@ImportResource(locations = {"classpath:coummer.xml"})
public class TestController extends UserController {
    @Autowired
    private ManagerUser managerUser;
    @Autowired
    private UserLogin userLogin;

    @RequestMapping(value = "/")
    public String go(Model model, HttpServletRequest request) {
        String message = (String) request.getSession().getAttribute("message");
        if (null != message)
            model.addAttribute("message", message);
        else {
            model.addAttribute("message", "");
        }
        return "login";
    }

    @RequestMapping(value = "/login")
    public String login(@Param("id") String id, @Param("password") String password, Model model, HttpServletRequest request) {
        User user = new SimpleUser();
        user.setId(id);
        user.setPassword(password);
        user = userLogin.login(user);
        if (null != user) {
            request.getSession().setAttribute("user", user);
            model.addAttribute("user", user);
        } else {
            request.getSession().setAttribute("message", "用户名或者密码错误");
            return "redirect:/";
        }
        return "redirect:/success?id=" + id;
    }


    @RequestMapping(value = "/save")
    public String save(@Param("id") String id, @Param("name") String name, @Param("password") String password, Model model) {
        User user = new SimpleUser();
        user.setName(name);
        user.setId(id);
        user.setPassword(password);
        user.setUserEnum(UserEnum.Simple);
        boolean b = managerUser.save(user);
        model.addAttribute("user", user);
        return b ? "redirect:/" + SUCCESS + "?id=" + id : "redirect:/" + ERROR;
    }

    @RequestMapping(value = "/saveAdmin")
    public String saveAdmin(Model model){
        User user = new AdminUser();
        user.setName("admin");
        user.setId("123456");
        user.setPassword("123456");
        user.setUserEnum(UserEnum.Admin);
        boolean b = managerUser.save(user);
        model.addAttribute("user", user);
        return b ? "redirect:/" + SUCCESS + "?id=123456" : "redirect:/" + ERROR;
    }
    @RequestMapping(value = "/saveSuper")
    public String saveSuper(Model model){
        User user = new AdminUser();
        user.setName("super");
        user.setId("1234567");
        user.setPassword("123456");
        user.setUserEnum(UserEnum.Super);
        boolean b = managerUser.save(user);
        model.addAttribute("user", user);
        return b ? "redirect:/" + SUCCESS + "?id=1234567" : "redirect:/" + ERROR;
    }

    @RequestMapping(value = "/updateUser")
    public String update(@Param("id") String id, @Param("name") String name, @Param("password") String password,
                         Model model, HttpServletRequest request) {
        User user = new User();
        user.setId(id);
        user.setName(name);
        user.setPassword(password);
        user.setUserEnum(UserEnum.Simple);
        boolean b = managerUser.save(user);
        model.addAttribute("user", user);
        return b ? "redirect:/" + SUCCESS + "?id=" + id : "redirect:/" + ERROR;
    }

    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public String update(Model model, HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        model.addAttribute("user", user);
        return "update";
    }

    @RequestMapping(value = "/add")
    public String add(Model model, HttpServletRequest request) {
        model.addAttribute("user", (User) request.getSession().getAttribute("user"));
        return "add";
    }

    @RequestMapping(value = "/register")
    public String register(Model model, HttpServletRequest request) {
        return "register";
    }

    @RequestMapping(value = "/success")
    public String success(@Param("id") String id, Model model) {
        User user = managerUser.findById(id);
        String result;
        if (!UserUtils.isEmpty(user)) {
            switch (user.getUserEnum()) {
                case Simple:
                    result = SIMPLE;
                    break;
                case Admin:
                    result = ADMIN;
                    break;
                case Super:
                    result = SUPER;
                    List<User> users = managerUser.users();
                    model.addAttribute("users", users);
                    break;
                default:
                    result = "redirect:/" + SUCCESS+ "?id=" + id ;
            }
            model.addAttribute("user", user);
            return result;
        }
        return "redirect:/";
    }
}